using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.ComponentModel;
using System.Collections.Generic;
using System.Security.Permissions;
using System.Drawing.Design;
using System.Web;
using System.Web.UI;
using System.Web.UI.Design;

[assembly: WebResource("Ajax.Logging.Scripts.WebServiceListener.js", "text/javascript")]


namespace Ajax.Logging
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class WebServiceListener : BaseListener
    {
        private const int DefaultInterval = (10 * 1000);
        private const int MinimumInterval = (3 * 1000);

        private string _servicePath = string.Empty;
        private string _serviceMethod = string.Empty;
        private int _logInterval = DefaultInterval;

        [DefaultValue("")]
        [Category("Behavior")]
        [Editor(typeof(System.Web.UI.Design.UrlEditor), typeof(UITypeEditor))]
        public string ServicePath
        {
            [DebuggerStepThrough()]
            get
            {
                return _servicePath;
            }
            [DebuggerStepThrough()]
            set
            {
                if (_servicePath != value)
                {
                    _servicePath = value;
                }
            }
        }

        [DefaultValue("")]
        [Category("Behavior")]
        public string ServiceMethod
        {
            [DebuggerStepThrough()]
            get
            {
                return _serviceMethod;
            }
            [DebuggerStepThrough()]
            set
            {
                if (_serviceMethod != value)
                {
                    _serviceMethod = value;
                }
            }
        }

        [DefaultValue(DefaultInterval)]
        [Category("Behavior")]
        public int LogInterval
        {
            [DebuggerStepThrough()]
            get
            {
                return _logInterval;
            }
            [DebuggerStepThrough()]
            [SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters")]
            set
            {
                if (value < MinimumInterval)
                {
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Interval must be greater than or equal to {0} miliseconds.", MinimumInterval.ToString(CultureInfo.CurrentCulture)));
                }

                _logInterval = value;
            }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override string CreateScript
        {
            [SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters")]
            [DebuggerStepThrough()]
            get
            {
                if (string.IsNullOrEmpty(ServicePath))
                {
                    throw new InvalidOperationException("ServicePath must be specified.");
                }

                if (string.IsNullOrEmpty(ServiceMethod))
                {
                    throw new InvalidOperationException("ServiceMethod must be specified.");
                }

                string script = string.Format(CultureInfo.InvariantCulture, "Ajax.Logging.ExceptionManager.getInstance().addListener(new {0}('', '{1}', '{2}', {3}));", this.GetType().FullName, Owner.Page.ResolveClientUrl(ServicePath), ServiceMethod, LogInterval.ToString(CultureInfo.InvariantCulture));

                return script;
            }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override IEnumerable<ScriptReference> ScriptReferences
        {
            [DebuggerStepThrough()]
            get
            {
                return new ScriptReference[] { new ScriptReference(Owner.Page.ClientScript.GetWebResourceUrl(this.GetType(), "Ajax.Logging.Scripts.WebServiceListener.js")) };
            }
        }
    }
}